/**
 * 
 */
package bean;

/**
 * The user profile bean.
 * 
 * @author chuxiaoyuan
 *
 */
public class UserProfile {

	/**
	 * Constructor.
	 */
	public UserProfile() {
		//
	}

	/**
	 * Constructor.
	 */
	public UserProfile(User user) {
		this.setUserInfo(user);
	}
	
	/**
	 * Constructor.
	 */
	public UserProfile(User user, Customer customer) {
		this.setUserInfo(user);
		this.setCustomerInfo(customer);
	}
	
	/**
	 * The user information.
	 */
	private User user;

	/**
	 * The customer information.
	 */
	private Customer customer;
	
	/**
	 * Get the user name.
	 * 
	 * @return the user
	 */
	public String getUsername() {
		return user.getAccount();
	}
	
	/**
	 * Get user password.
	 * 
	 * @return password.
	 */
	public String getPassword() {
		return user.getPassword();
	}
	
	/**
	 * Test if this user is a customer.
	 * 
	 * @return true if it is a customer.
	 */
	public boolean isCustomer() {
		return this.user.getCustomerid() != null;
	}

	/**
	 * Test if this user is an administrator.
	 * 
	 * @return true if it is an administrator.
	 */
	public boolean isAdmin() {
		return !this.isCustomer();
	}
	
	/**
	 * Get customer id.
	 * 
	 * @return customer id. null if not a customer.
	 */
	public Long getCustomerid() {
		return this.user.getCustomerid();
	}
	

	/**
	 * Get customer name.
	 * 
	 * @return customer name.
	 */
	public String getCustomerName() {
		if (this.customer == null) {
			return null;
		}
		return this.customer.getName();
	}

	/**
	 * Set customer Name.
	 * 
	 * @param customerName the customer name.
	 */
	public void setCustomerName(String customerName) {

		if (this.customer == null) {
			return;
		}
		this.customer.setName(customerName);
	}
	
	/**
	 * Get customer address.
	 * 
	 * @return customer address.
	 */
	public String getCustomerAddress() {
		if (this.customer == null) {
			return null;
		}
		return this.customer.getAddress();
	}

	/**
	 * Set customer address.
	 * 
	 * @param customerAddress the customer address.
	 */
	public void setCustomerAddress(String customerAddress) {

		if (this.customer == null) {
			return;
		}
		this.customer.setAddress(customerAddress);
	}
	
	/**
	 * Get customer telephone.
	 * 
	 * @return customer telephone.
	 */
	public String getCustomerTelephone() {
		if (this.customer == null) {
			return null;
		}
		return this.customer.getTelephone();
	}

	/**
	 * Set customer Telephone.
	 * 
	 * @param customerTelephone the customer Telephone.
	 */
	public void setCustomerTelephone(String customerTelephone) {

		if (this.customer == null) {
			return;
		}
		this.customer.setTelephone(customerTelephone);
	}
	
	/**
	 * Set user info.
	 * @param user the user to set
	 */
	public void setUserInfo(User user) {
		this.user = user;
	}
	
	/**
	 * Set customer.
	 * Note. User information needs to be set before customer information.
	 * 
	 * @param customer the customer to set
	 * 
	 * @return false if invalid customer info.
	 */
	public boolean setCustomerInfo(Customer customer) {
		
		if (user == null) {
			return false;
		}
		if (user.getCustomerid() == null) {
			
			if (customer != null && customer.getId() != null) {
				return false;
			}
			
		} else {
			
			if (customer == null || customer.getId() == null) {
				return false;
				
			} else if (!customer.getId().equals(user.getCustomerid())) {
				return false;
			}
		}

		// Set customer.
		this.customer = customer;
		return true;
	}

	/**
	 * Get user information.
	 * 
	 * @return user information.
	 */
	public User getUserInfo() {
		return this.user;
	}

	/**
	 * Get customer information.
	 * 
	 * @return customer information.
	 */
	public Customer getCustomerInfo() {
		return this.customer;
	}
	
}
